package com.longli.tank;

import java.awt.*;
import java.util.Random;


public class Tank extends AbstractGameObject{
    private int x,y;
    private final  int SPEED=5;
    private Dir dir;
    private boolean moving=true;
    private boolean bL,bR,bU,bD;
    private Group group;
    private boolean live=true;
    private int oldX,ordY;
    private int width,height;

    public Tank(int x,int y,Dir dir,Group group) {
        this.x=x;
        this.y=y;
        this.dir=dir;
        this.group=group;
        oldX=x;
        ordY=y;
        this.width=ResourceMgr.goodTankU.getWidth();
        this.height=ResourceMgr.goodTankU.getHeight();

    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    @Override
    public boolean isLive() {
        return live;
    }

    public void setLive(boolean live) {
        this.live = live;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    @Override
    public void paint(Graphics g){
        if(!this.isLive()){return;}
        switch (dir){
            case L:
                g.drawImage(ResourceMgr.badTankL,x,y,null);
                break;
            case R:
                g.drawImage(ResourceMgr.badTankR,x,y,null);
                break;
            case U:
                g.drawImage(ResourceMgr.badTankU,x,y,null);
                break;
            case D:
                g.drawImage(ResourceMgr.badTankD,x,y,null);
                break;
        }
        move();
    }

    private void move() {
        oldX=x;
        ordY=y;
        if(!moving){return;}

        switch (dir){
            case L:
                x-=SPEED;
                break;
            case R:
                x+=SPEED;
                break;
            case U:
                y-=SPEED;
                break;
            case D:
                y+=SPEED;
                break;
        }
        boundCheck();
        randomDir();
        if(r.nextInt(100)>90){
            fire();
        }
    }
    private Random r=new Random();
    public void randomDir(){
        if(r.nextInt(100)>90){
            this.dir=Dir.randomDir();
        }

    }


    private void setMainDir() {
        if(!bR && !bL && !bU&& !bD){
            moving=false;
        }else {
            moving=true;
            if(bR && !bL && !bU&& !bD){
                dir=Dir.R;
            }
            if(!bR && bL && !bU&& !bD){
                dir=Dir.L;
            }
            if(!bR && !bL && bU&& !bD){
                dir=Dir.U;
            }
            if(!bR && !bL && !bU&& bD){
                dir=Dir.D;
            }
        }

    }

    public void fire(){
        int bx=x+ResourceMgr.goodTankL.getWidth()/2-ResourceMgr.bulletL.getWidth()/2;
        int by=y+ResourceMgr.goodTankL.getHeight()/2-ResourceMgr.bulletL.getHeight()/2;
        Dir[] dirs=Dir.values();
        for(Dir d:dirs){
            TankFrame.INSTANCE.add(new Bullet(bx,by,dir,group));
        }

    }
    public void die(){
        this.setLive(false);
        TankFrame.INSTANCE.add(new Explode(x,y));

    }

    private void boundCheck() {
        if(x<0||y<30||x+width>TankFrame.INSTANCE.GAME_WIDTH||y+height>TankFrame.INSTANCE.GAME_HEIGHT){
            this.back();

        }
    }

    private void back() {
        this.x=oldX;
        this.y=ordY;
    }


}
